package com.lianzhihui.romeex.ui.wallet;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lianzhihui.romeex.R;
import com.lianzhihui.romeex.adapter.TradeRecordAdapter;
import com.lianzhihui.romeex.bean.TradeRecordBean;
import com.lianzhihui.romeex.bean.TradeRecordEarningsBean;
import com.lianzhihui.romeex.config.AppConstants;
import com.lianzhihui.romeex.mvp.presenter.TradeRecordPresnterImp;
import com.lianzhihui.romeex.mvp.view.ZZRecordView;
import com.lianzhihui.romeex.ui.base.BaseActivity;
import com.lianzhihui.romeex.utils.TextUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Description: 转账记录
 * Author: RAMON
 * CreateDate: 2020/11/6 10:20 AM
 * UpdateUser:
 * UpdateDate:
 * UpdateRemark:
 * Version:
 */
public class TradeRecordActivity extends BaseActivity implements ZZRecordView, OnRefreshLoadMoreListener, BaseQuickAdapter.OnItemClickListener {

    @BindView(R.id.refresher)
    SmartRefreshLayout refresher;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private static final int pageSize = 20;
    TradeRecordAdapter adapter;
    private TradeRecordPresnterImp zzRecordPresnterImp;
    private String coinName;
    private String coinNameCobo;
    private String coinNameAlias;
    private String tradeType;
    private TradeRecordBean data;
    private List<TradeRecordBean.DetailBean> content;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_zz_record_layout;
    }

    @Override
    protected void initView() {
        setTitle("转账记录");
        Intent intent = getIntent();
        coinName = intent.getStringExtra("coinName");
        coinNameCobo = intent.getStringExtra("coinNameCobo");
        coinNameAlias = intent.getStringExtra("coinNameAlias");
        //交易类型 0 全部 1 转账 2 收款 3 划转
        tradeType = intent.getStringExtra("tradeType");
        if (TextUtils.isEmpty(tradeType)) {
            tradeType = "0";
        }
        if (tradeType.equals("1")) {
            setTitle("转账记录");
        }
        if (tradeType.equals("2")) {
            setTitle("收款记录");
        }

        refresher.setOnRefreshLoadMoreListener(this);
    }

    @Override
    protected void initData() {
        zzRecordPresnterImp = new TradeRecordPresnterImp(this, this);
        initRecyclerView();
        refresher.autoRefresh();
    }

    private void initRecyclerView() {
        if (adapter == null) {
            adapter = new TradeRecordAdapter(new ArrayList<>());
            adapter.setCoinName(coinName);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
            linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
            recyclerView.setLayoutManager(linearLayoutManager);
            recyclerView.setAdapter(adapter);
            adapter.setEmptyView(getEmptyView());
        } else {
            adapter.setNewData(content);
            adapter.setCoinName(data.coinName);
        }
        adapter.setOnItemClickListener(this);
    }

    @Override
    public void success(TradeRecordBean data) {
        refresher.finishLoadMore();
        refresher.finishRefresh();
        if (data != null) {
            this.data = data;
            List<TradeRecordBean.DetailBean> temp;
            temp = data.getContent();
            if (temp.size() > 0) {
                if (pageNum == 1) {
                    adapter.setNewData(temp);
                } else {
                    adapter.addData(temp);
                }
                pageNum++;
                content = adapter.getData();
            }
        }
    }

    @Override
    public void responseWalletProfitlistSuccess(TradeRecordEarningsBean data) {

    }

    @Override
    public void failed(int errorCode, String str) {
        if (errorCode == -1) {
            //网络异常
        } else {
            //服务器错误码消息
            toast(str);
        }
        refresher.finishLoadMore();
        refresher.finishRefresh();
    }

    @Override
    public void setFailure() {
        refresher.finishLoadMore();
        refresher.finishRefresh();
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        getData();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        pageNum = 1;
        getData();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Intent intent = new Intent(this, TradeRecordDetailsActivity.class);
        TradeRecordBean.DetailBean item = (TradeRecordBean.DetailBean) adapter.getItem(position);
        intent.putExtra(AppConstants.RECORD_DETAILS_KEY, item.getTransactionHashcode());
        intent.putExtra("tradeType", tradeType);
        intent.putExtra("coinName", coinName);
        startActivity(intent);
//        IntentUtils.JumpToHaveObj(TradeRecordDetailsActivity.class, AppConstants.RECORD_DETAILS_KEY, content.get(position).getTransactionHashcode());
    }

    private void getData() {
        zzRecordPresnterImp.requestTradeRecord(pageNum, pageSize, coinName, coinNameCobo, tradeType);
    }

}
